package me.msz.application.m2copy.core.impl;

import ch.qos.logback.classic.LoggerContext;
import me.msz.application.m2copy.core.Copyable;
import me.msz.application.m2copy.core.M2RepoCopier;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;

/**
 * Created by MeiSZ on 2016/12/12.
 */
public class DefaultCopyable implements Copyable {
    private Logger logger = LoggerFactory.getLogger(DefaultCopyable.class);
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();

    private File file;

    private DefaultCopyable() {

    }

    public DefaultCopyable(File file) {
        this.file = file;
    }

    @Override
    public boolean isCopied() {
        return new File(this.file.getParentFile().getAbsolutePath() + File.separator + "." + this.file.getName() + ".copied").exists();
    }

    @Override
    public void setCopied() {
        if(!this.isCopied()) {
            try {
                logger.debug("flag file name is " + this.file.getParentFile().getAbsolutePath() + File.separator + "." + this.file.getName() + ".copied");
                File flagFile = new File(this.file.getParentFile().getAbsolutePath() + File.separator + "." + this.file.getName() + ".copied");
                FileUtils.touch(flagFile);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
